package com.tuyrt.`as`.proj.main.fragment.home

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tuyrt.`as`.proj.common.ui.view.loadUrl
import com.tuyrt.`as`.proj.main.R
import com.tuyrt.`as`.proj.main.model.Subcategory
import com.tuyrt.`as`.proj.main.route.HiRoute
import com.tuyrt.`as`.proj.main.route.HiRoute.Destination.GOODS_LIST
import com.tuyrt.hi.library.util.HiDisplayUtil
import com.tuyrt.hi.library.util.HiRes
import com.tuyrt.hi.ui.item.HiDataItem
import com.tuyrt.hi.ui.item.HiViewHolder

/**
 * author: Administrator on 2021/7/6.
 * desc:
 */
class GridItem(val list: List<Subcategory>) : HiDataItem<List<Subcategory>, HiViewHolder>(list) {

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context
        val gridView = holder.itemView as RecyclerView
        gridView.adapter = GridAdapter(context, list)
    }

    override fun getItemView(parent: ViewGroup): View? {
        val gridView = RecyclerView(parent.context)
        val params = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT)
        params.bottomMargin = HiDisplayUtil.dp2px(10f)
        gridView.layoutManager = GridLayoutManager(parent.context, 5)
        gridView.layoutParams = params
        gridView.setBackgroundColor(HiRes.getColor(R.color.color_white))
        return gridView
    }


    inner class GridAdapter(val context: Context, val list: List<Subcategory>) : RecyclerView.Adapter<HiViewHolder>() {
        private var mInflater: LayoutInflater

        init {
            mInflater = LayoutInflater.from(context)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HiViewHolder {
            val view = mInflater.inflate(R.layout.layout_home_op_grid_item, parent, false)
            return HiViewHolder(view)
        }

        override fun onBindViewHolder(holder: HiViewHolder, position: Int) {
            val subcategory = list[position]

            holder.findViewById<ImageView>(R.id.item_image)?.loadUrl(subcategory.subcategoryIcon)
            holder.findViewById<TextView>(R.id.item_title)?.text = subcategory.subcategoryName

            //holder.itemView.item_image.loadUrl(subcategory.subcategoryIcon)
            //holder.itemView.item_title.text = subcategory.subcategoryName

            holder.itemView.setOnClickListener {
                // 会跳转到子分类列表
                //Toast.makeText(context, "会跳转到子分类列表:$position", Toast.LENGTH_SHORT).show()

                val bundle = Bundle()
                bundle.putString("categoryId", subcategory.categoryId)
                bundle.putString("subcategoryId", subcategory.subcategoryId)
                bundle.putString("categoryTitle", subcategory.subcategoryName)
                HiRoute.startActivity(context, bundle, GOODS_LIST)
            }
        }

        override fun getItemCount(): Int {
            return list.size
        }
    }

}